package com.gx.gxreader.fragment

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.widget.RadioGroup
import com.gx.gxreader.R
import com.gx.gxreader.activity.SearchActivity
import com.gx.gxreader.base.BaseFragment
import com.gx.gxreader.presenter.StackRoomPresenter
import com.gx.gxreader.view.StackRoomView
import kotlinx.android.synthetic.main.fragment_stackroom.*


/**
 * 书库
 */
class StackRoomFragment : BaseFragment<StackRoomView, StackRoomPresenter>(), StackRoomView, RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    val list = arrayListOf<Fragment>()

    override fun getContentViewId(): Int = R.layout.fragment_stackroom

    override fun createPresenter(): StackRoomPresenter = StackRoomPresenter()

    override fun initView() {
        list.add(ClassificationFragment())
        list.add(ClassificationFragment())
        list.add(ClassificationFragment())
        list.add(ClassificationFragment())
        list.add(ClassificationFragment())
        vpStackroom.adapter = MyViewPagerAdapter(list, activity!!.supportFragmentManager)

        rbPublis.setTextSize(20F)
        rbSole.setTextSize(15F)
        rbBoy.setTextSize(15F)
        rbGirl.setTextSize(15F)
        rbVip.setTextSize(15F)
        vpStackroom.setCurrentItem(0)
    }

    override fun initListener() {
        rgStackRoom.setOnCheckedChangeListener(this)
        vpStackroom.setOnPageChangeListener(this)

        tvSearchBookName.setOnClickListener{
            val intent = Intent()
            intent.setClass(activity, SearchActivity::class.java)
            startActivity(intent)
        }
    }

    private class MyViewPagerAdapter(var list : ArrayList<Fragment>, fm : FragmentManager) : FragmentPagerAdapter(fm) {
        override fun getItem(p0: Int): Fragment = list[p0]

        override fun getCount(): Int = list.size
    }

    override fun onCheckedChanged(p0: RadioGroup?, p1: Int) {
        when (p1) {
            R.id.rbPublis -> {
                rbPublis.setTextSize(20F)
                rbSole.setTextSize(15F)
                rbBoy.setTextSize(15F)
                rbGirl.setTextSize(15F)
                rbVip.setTextSize(15F)
                vpStackroom.setCurrentItem(0)
            }
            R.id.rbSole -> {
                rbPublis.setTextSize(15F)
                rbSole.setTextSize(20F)
                rbBoy.setTextSize(15F)
                rbGirl.setTextSize(15F)
                rbVip.setTextSize(15F)
                vpStackroom.setCurrentItem(1)
            }
            R.id.rbBoy -> {
                rbPublis.setTextSize(15F)
                rbSole.setTextSize(15F)
                rbBoy.setTextSize(20F)
                rbGirl.setTextSize(15F)
                rbVip.setTextSize(15F)
                vpStackroom.setCurrentItem(2)
            }
            R.id.rbGirl -> {
                rbPublis.setTextSize(15F)
                rbSole.setTextSize(15F)
                rbBoy.setTextSize(15F)
                rbGirl.setTextSize(20F)
                rbVip.setTextSize(15F)
                vpStackroom.setCurrentItem(3)
            }
            R.id.rbVip -> {
                rbPublis.setTextSize(15F)
                rbSole.setTextSize(15F)
                rbBoy.setTextSize(15F)
                rbGirl.setTextSize(15F)
                rbVip.setTextSize(20F)
                vpStackroom.setCurrentItem(4)
            }
        }
    }

    override fun onPageScrollStateChanged(p0: Int) {

    }

    override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

    }

    override fun onPageSelected(p0: Int) {
        rgStackRoom.check(rgStackRoom.getChildAt(p0).id)
    }
}